package tv.youxing.android.ui;
/**
 * Created by apple on 2017/4/24.
 */

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import tv.youxing.android.R;
import tv.youxing.android.custom.CustomCountDownTimer;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.PhoneCodeModel;
import tv.youxing.android.entity.UserPwdLoginModel;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.L;
import tv.youxing.android.utils.StaticClass;
import tv.youxing.android.utils.UtilTools;

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .ui
 * 文件名：   RegisterActivity
 * 创建者：   叶克武
 * 创建时间： 2017/4/24 下午6:58
 * 描述：    注册
 */
public class RegisterActivity extends BaseActivity {


    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.sendCode)
    EditText sendCode;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.nickname)
    EditText nickname;
    @BindView(R.id.tv_registerProtocal)
    TextView tvRegisterProtocal;
    @BindView(R.id.Protocal_iv_select)
    ImageView ProtocalIvSelect;
    @BindView(R.id.btn_sendCode)
    Button btn_senCode;

    boolean is_protocal_iv_selector=  false;//用户是否同意注册协议

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initView();



    }

    private void initView() {


    }

    @OnClick({R.id.btn_sendCode, R.id.btn_login, R.id.Protocal_iv_select,R.id.tv_registerProtocal})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.btn_sendCode:
                requestCode(phone.getText().toString().trim());
                break;
            case R.id.btn_login:
                login();
                break;
            case R.id.Protocal_iv_select:
                is_protocal_iv_selector=!is_protocal_iv_selector;

                if (is_protocal_iv_selector) {
                    ProtocalIvSelect.setImageResource(R.drawable.batch_icon_download_pre);
                }else{
                    ProtocalIvSelect.setImageResource(R.drawable.batch_icon_download_nor);
                }
                break;
            case R.id.tv_registerProtocal:
                // TODO: 2017/6/15 用户协议界面
                Intent intent = new Intent(this, AdvertisementWEBActivity.class);
                intent.putExtra("url",StaticClass.registerAgreement);
                intent.putExtra("title","注册协议");
                startActivity(intent);
                break;
        }
    }


    private void login() {
        if (!TextUtils.isEmpty(phone.getText().toString()) &&
                !TextUtils.isEmpty(sendCode.getText().toString()) &&
                !TextUtils.isEmpty(password.getText().toString()) &&
                !TextUtils.isEmpty(nickname.getText().toString())
                ) {
            String pahone = phone.getText().toString().trim();
            String code = sendCode.getText().toString().trim();
            String pwd = password.getText().toString().trim();
            String name = nickname.getText().toString().trim();

            if (!UtilTools.isPhoneSuccess(pahone)) {
                CustomToast.INSTANCE.showToast("手机号码格式不正确");
                return;
            }


            if (!UtilTools.isPwdSuccess(pwd)) {
                CustomToast.INSTANCE.showToast("密码格式不正确,请包含数字和字母,长度至少为8位");
                return;
            }


            if (!is_protocal_iv_selector) {
                CustomToast.INSTANCE.showToast("请阅读用户协议");
                return;
            }

            //post请求简洁版实现
            HttpParams params = new HttpParams();
            params.put("mobile", pahone);
            params.put("code", code);
            params.put("password", pwd);
            params.put("username", name);


            RxVolley.post(StaticClass.registerURL, params, new HttpCallback() {
                @Override
                public void onSuccess(String t) {
                    // TODO: 2017/6/20 登录完成进入主页


                    UserPwdLoginModel userPwdLoginModel = GsonUtils.GsonToBean(t, UserPwdLoginModel.class);
                    CustomToast.INSTANCE.showToast(userPwdLoginModel.getStatus());
                    if (userPwdLoginModel.getCode() == 200) {

                    }
                    CustomToast.INSTANCE.showToast(userPwdLoginModel.getStatus());
                    startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
                    finish();
                }
                
                

                @Override
                public void onFailure(VolleyError error) {
                    L.d("error:" + error.getMessage());
                    CustomToast.INSTANCE.showToast(error.getMessage());
                }
            });

        } else {
            CustomToast.INSTANCE.showToast("请填写完整");
        }

    }

    void requestCode(String phone) {
        if (!UtilTools.isPhoneSuccess(phone)) {
            CustomToast.INSTANCE.showToast("手机号码不正确");
            return;
        }

        HttpParams params = new HttpParams();

        params.put("mobile", phone);

        RxVolley.post(StaticClass.sendCheckCodeURL, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
//                    CustomToast.INSTANCE.showToast("onSuccess:"+t);

                PhoneCodeModel pd =GsonUtils.GsonToBean(t, PhoneCodeModel.class);

                CustomToast.INSTANCE.showToast(pd.getStatus());

                if (pd.getCode() == 200) {
                    CustomCountDownTimer customCountDownTimer = new CustomCountDownTimer(60000,1000,btn_senCode);
                    customCountDownTimer.start();
                }




//                L.d("onSuccess:");



            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                L.d("errorCode:" + error.getMessage());
                CustomToast.INSTANCE.showToast(error.getMessage());
            }
        });
    }
}
